package com.xuxu.controller;

import com.xuxu.pojo.User;
import com.xuxu.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * controller层
 */

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    public String add(@Param("User")  User user){
        userService.add(user);
        return "seccuss";
    }

    @RequestMapping("/file")
    public String file(MultipartFile file){

        //获取源文件名
         String originalFilename = file.getOriginalFilename();
        //获取文件的后缀名
        String fileName = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        System.out.println("文件名称是："+fileName);

        try {
            file.transferTo(new File("/Users/maguoxu/"+File.separator+fileName));


        } catch (IOException e) {
            e.printStackTrace();
        }

        return "seccuss";
    }

}
